<?php

class PlanElementManager extends TModule
{
	// -------------------- basic functions --------------------
	/**
	 * @param integer $conference_element_id
	 * @return boolean
	 * @soapmethod
	 */
	public function addPlanElement($conference_element_id)
	{
		$element_manager = $this->Application->Modules['conference_elements'];
		$element = $element_manager->getConferenceElementById($conference_element_id);

		if($element)
		{
			$element->setIsPlan(1);
			if($element_manager->updateConferenceElementByConferenceElementRecord($element))
				return true;
		}
		return false;
	}
	
	/**
	 * @param integer $conference_element_id
	 * @return boolean
	 * @soapmethod
	 */
	public function deletePlanElement($conference_element_id)
	{
		$element_manager = $this->Application->Modules['conference_elements'];
		$element = $element_manager->getConferenceElementById($conference_element_id);

		if($element)
		{
			$element->setIsPlan(0);
			if($element_manager->updateConferenceElementByConferenceElementRecord($element))
				return true;
		}
		return false;
	}
	
	/**
	 * @param integer $user_id
	 * @param integer $conference_element_id
	 * @return boolean
	 * @soapmethod
	 */
	public function addUserPlanElement($user_id, $conference_element_id)
	{
		$element_manager = $this->Application->Modules['conference_elements'];
		$user_manager = $this->Application->Modules['users'];
		$user_element_manager = $this->Application->Modules['users_conference_elements'];
		
		if($user_element_manager->hasUserConferenceElement($user_id, $conference_element_id))
			return true;
		
		$user = $user_manager->getUserById($user_id);
		if($user)
		{
			if($this->checkUserRights($user_id))
			{
				$element = $element_manager->getConferenceElementById($conference_element_id);
				if($element)
				{
					if($this->canUsePlanElement($conference_element_id))
						if($user_element_manager->addUserConferenceElement($user_id, $conference_element_id))
							return true;
				}
			}
		}
		return false;
	}
	
	/**
	 * @param integer $user_id
	 * @param integer $conference_element_id
	 * @return boolean
	 * @soapmethod
	 */
	public function deleteUserPlanElement($user_id, $conference_element_id)
	{
		$user_element_manager = $this->Application->Modules['users_conference_elements'];
		return $user_element_manager->deleteUserConferenceElement($user_id, $conference_element_id);
	}
	
	/**
	 * @param integer $user_id
	 * @param integer $conference_element_id
	 * @return boolean
	 * @soapmethod
	 */
	public function hasUserPlanElement($user_id, $conference_element_id)
	{
		$user_element_manager = $this->Application->Modules['users_conference_elements'];
		return $user_element_manager->hasUserConferenceElement($user_id, $user_element_manager);
	}

	/**
	 * @param integer $user_id
	 * @return boolean
	 * @soapmethod
	 */
	public function checkUserRights($user_id)
	{
		$role_manager = $this->Application->Modules['roles'];
		$user_role_manager = $this->Application->Modules['users_roles'];
		
		if(	$user_role_manager->hasUserRole($user_id, $role_manager->getRoleByTitle(RoleManager::$PARTICIPANT_ROLE)->getId())	||
			$user_role_manager->hasUserRole($user_id, $role_manager->getRoleByTitle(RoleManager::$COMMITTEE_ROLE)->getId())		||
			$user_role_manager->hasUserRole($user_id, $role_manager->getRoleByTitle(RoleManager::$ORGANIZER_ROLE)->getId())		||
			$user_id == 1) // when admin
				return true;
		return false;
	}
	
	/**
	 * @param integer $conference_element_id
	 * @return boolean
	 * @soapmethod
	 */
	public function canUsePlanElement($conference_element_id)
	{
		$element_manager = $this->Application->Modules['conference_elements'];
		$user_element_manager = $this->Application->Modules['users_conference_elements'];
		
		$element = $element_manager->getConferenceElementById($conference_element_id);
		if($element)
		{
			if($element->getIsPlan())
			{
				if($element->getQuantity() != null)
				{
					$quantity = $user_element_manager->
									countUserConferenceElementsByConferenceElementId($conference_element_id);
					// if is has free seats
					if($quantity < $element->getQuantity())
						return true;
				}
				else
					return true;			
			}
		}
		return false;
	}
	
	// -------------------- extended functions --------------------
	/**
	 * @return ConferenceElementRecord[]
	 * @soapmethod
	 */
	public function getPlanElementsList()
	{
		$element_manager = $this->Application->Modules['conference_elements'];
		return $element_manager->getConferenceElementsListByIsPlan(1, false, -1, -1);
	}
	
	/**
	 * @return ConferenceElementRecord[]
	 * @soapmethod
	 */
	public function getNotPlanElementsList()
	{
		$element_manager = $this->Application->Modules['conference_elements'];
		return $element_manager->getConferenceElementsListByIsPlan(0, false, -1, -1);
	}
	
	/**
	 * @return integer
	 * @soapmethod
	 */
	public function countPlanElements()
	{
		$element_manager = $this->Application->Modules['conference_elements'];
		return $element_manager->countConferenceElementsByIsPlan(1);
	}
	
	/**
	 * @return integer
	 * @soapmethod
	 */
	public function countNotPlanElements()
	{
		$element_manager = $this->Application->Modules['conference_elements'];
		return $element_manager->countConferenceElementsByIsPlan(0);
	}

	/**
	 * @param integer $user_id
	 * @return ConferenceElementRecord[]
	 * @soapmethod
	 */
	public function getPlanElementsListByUserId($user_id)
	{
		$sql = 	'SELECT `conference_elements`.*'.
				' FROM `conference_elements` JOIN `users_conference_elements`'.
				' ON conference_elements.id = users_conference_elements.conference_element_id'.
				' WHERE users_conference_elements.user_id = \''.$user_id.'\'';
				
		return ConferenceElementRecord::finder()->with_place()->findAllBySql($sql);
	}
	
	/**
	 * @param integer $user_id
	 * @return integer
	 * @soapmethod
	 */
	public function countPlanElementsByUserId($user_id)
	{
		$user_element_manager = $this->Application->Modules['users_conference_elements'];
		return $user_element_manager->countUserConferenceElementsByUserId($user_id);
	}
	
	/**
	 * @return ConferenceElementRecord[]
	 * @soapmethod
	 */
	public function getConferenceElementsList()
	{
		$element_manager = $this->Application->Modules['conference_elements'];
		return $element_manager->getConferenceElementsList(false, -1, -1);
	}
	
	/**
	 * @return integer
	 * @soapmethod
	 */
	public function countConferenceElements()
	{
		$element_manager = $this->Application->Modules['conference_elements'];
		return $element_manager->countConferenceElements();
	}
	
	
	// -------------------- different functions --------------------
	/**
	 * @return string
	 * @soapmethod
	 */
	public function getCurrecyName()
	{
		$setting_manager = $this->Application->Modules['settings'];
		if($setting = $setting_manager->getSettingByName(SettingManager::$CURRENCY_NAME))
			return $setting->getValue() != '' ? $setting->getValue() : '-';
		return '-';
	}
	
	/**
	 * @param integer $conference_element_id
	 * @return string
	 * @soapmethod
	 */
	public function getShowConferenceElementUrl($conference_element_id)
	{
		return $this->Request->constructUrl('page', 'conference_elements.Show',
											array('id' => $conference_element_id));
	}
}

?>